package 周赛;

import org.junit.jupiter.api.Test;

public class 数组美丽值求和 {

    public int sumOfBeauties(int[] nums) {
        int n = nums.length;
        //找到每个数字右边最小值
        int[] minRight = new int[n];
        int min=nums[n-1];
        //记录每个数字的美丽值
        int[] res = new int[n];
        for (int i = n-2; i >=0 ; i--) {
            minRight[i] = min;
            min=Math.min(min,nums[i]);
        }
        //左边最大值
        int max=nums[0];
        for (int i = 1; i < n-1 ; i++) {
            if (nums[i]>max && nums[i]<minRight[i])
                res[i] = 2;
            else if (nums[i]>nums[i-1] && nums[i]<nums[i+1])
                res[i] = 1;
            else
                res[i]=0;
            max=Math.max(max,nums[i]);
        }
        int ans=0;
        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
            ans+=res[i];
        }
        return ans;
    }
    @Test
    public void test(){
        int[] nums={9,9,3,8,7,9,6,10};
        System.out.println(sumOfBeauties(nums));
    }
}
